{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# 构建一个代理(agent)\n",
    "\n",
    "单靠语言模型无法才去行动-它们只是输出文本。LangChain的一个重要用例是创建代理(agent)。代理是使用大型语言模型作为推理引擎的系统，以确定才去哪些行动以及传递给他们的输入。在执行操作后，结果可以反馈到大型语言模型中，以确定是否需要更多操作，或者是否可以结束。\n",
    "\n",
    "在本教程中，我们将构建一个可以与搜索引擎互动的代理。您将能够向这个代理提问，观看它调用搜索工具，并与它进行对话。"
   ],
   "id": "6d5967378962cf64"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# 端到端代理\n",
    "\n",
    "下面的代码片段表示一个完全功能的代理，它使用大型语言模型来决定使用哪些工具。它配备了一个通用搜索工具。它具有对话记忆-这意味着它可以作为一个多轮聊天机器人使用。\n",
    "\n",
    "在本指南的其余部门，我们将逐步介绍各个组件及其功能-但如果您只想获取一些代码并开始使用，请随意使用这个！"
   ],
   "id": "77360af58f9715bf"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "from langchain_core.messages import HumanMessage\n",
    "#<!--IMPORTS:[{\"imported\": \"ChatAnthropic\", \"source\": \"langchain_anthropic\", \"docs\": \"https://python.langchain.com/api_reference/anthropic/chat_models/langchain_anthropic.chat_models.ChatAnthropic.html\", \"title\": \"Build an Agent\"}, {\"imported\": \"TavilySearchResults\", \"source\": \"langchain_community.tools.tavily_search\", \"docs\": \"https://python.langchain.com/api_reference/community/tools/langchain_community.tools.tavily_search.tool.TavilySearchResults.html\", \"title\": \"Build an Agent\"}, {\"imported\": \"HumanMessage\", \"source\": \"langchain_core.messages\", \"docs\": \"https://python.langchain.com/api_reference/core/messages/langchain_core.messages.human.HumanMessage.html\", \"title\": \"Build an Agent\"}]-->\n",
    "\n",
    "from langgraph.checkpoint.memory import MemorySaver\n",
    "from langchain_community.chat_models import ChatOpenAI\n",
    "from langchain_community.tools import TavilySearchResults\n",
    "from langgraph.prebuilt import create_react_agent\n",
    "from langchain.agents import tool\n",
    "\n",
    "\n",
    "# Create the agent\n",
    "memory = MemorySaver()\n",
    "\n",
    "# 使用 DeepSeek 的兼容 OpenAI 接口\n",
    "model = ChatOpenAI(\n",
    "    model=\"deepseek-chat\",\n",
    "    base_url=\"https://api.deepseek.com/v1\",  # DeepSeek 的 OpenAI 兼容接口\n",
    "    api_key=\"sk-a76a85b93093439ba3dc5b6dedfc51e5\",\n",
    "    temperature=0,\n",
    "    max_tokens=2048,\n",
    ")\n",
    "search = TavilySearchResults(max_results=2)\n",
    "tools = [search]\n",
    "agent_executor = create_react_agent(model, tools, checkpointer=memory)\n",
    "\n",
    "# Use the agent\n",
    "config = {\"configurable\": {\"thread_id\": \"abc123\"}}\n",
    "for chunk in agent_executor.stream(\n",
    "        {\"messages\": [HumanMessage(content=\"你好，我叫小明，我住在杭州。\")]},config\n",
    "):\n",
    "    print(chunk)\n",
    "    print(\"------\")"
   ],
   "id": "b2e9b8199a52a1e4",
   "outputs": [],
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
